﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ArcEngine.Controllers;
using ArcEngine.Views;
using DIWAGIS.ApplicationLayer.BanDo;
using DIWAGIS.ApplicationLayer.Commands;
using DIWAGIS.ApplicationLayer.DanhMuc;
using DIWAGIS.ApplicationLayer.EditorTools;
using DIWAGIS.ApplicationLayer.HeThong;
using DIWAGIS.ApplicationLayer.QuanLyMangCapNuoc;
using DIWAGIS.ApplicationLayer.ThongKeTaiSan;
using DIWAGIS.Class;
using DIWAGIS.Modules;
using ESRI.ArcGIS;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;
using Path = System.IO.Path;

namespace DIWAGIS
    {
    public partial class MainForm : Form
        {
        #region

        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam, uint fWinIni);

        /* constants used for user32 calls */
        private const uint SPI_GETFONTSMOOTHING = 74;
        private const uint SPI_SETFONTSMOOTHING = 75;
        private const uint SPIF_UPDATEINIFILE = 0x1;

        private static frmIndentifyFeature m_IdentifyFeature = null;
        private ITOCControl2 m_tocControl;
        private IMapControl3 m_mapControl;
        private IToolbarMenu m_menuMap;
        private IToolbarMenu m_menuLayer;

        private  string m_documentFileName = string.Empty;
        private ControlsSynchronizer m_controlsSynchronizer = null;
        private  IPageLayoutControl2 m_pageLayoutControl = null;
        private  FolderBrowserDialog folderBrowserDialogOpenMap = new FolderBrowserDialog();

        private DIWAGIS.ApplicationLayer.EditorTools.ToolCutPolygon ToolCutPolygon;
        private DIWAGIS.ApplicationLayer.Commands.cmdUpdateInfo cmdUpdateInfo;
        //Các biến thực hiện TOCcontectMenuStrip
        private esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
        private IBasicMap map = null;
        private ILayer layer = null;
        private object other = null;
        private object index = null;
        private ILayer m_TargetLayer;
        private IEngineEditor Editor;
        private Class.Editing editing;
        private IWorkspaceEdit WorkspaceEdit;
        private EngineEditorClass m_EngineEditor;
        #endregion

        public MainForm()
            {
            /****** Option 1: ******/
            RuntimeManager.BindLicense(ProductCode.EngineOrDesktop, LicenseLevel.GeodatabaseUpdate);
           // ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop); 
            InitializeComponent();
            m_EngineEditor=new EngineEditorClass();

            //// Usual engine initialization code follows from here (AoInitialize).
            IAoInitialize init = new AoInitializeClass();
            init.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
            init.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);
            init.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
            init.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcServer);
            }

        private void banDoToolStripMenuItem_Click(object sender, EventArgs e)
            {
            if (banDotoolStrip.Visible == false)
                {
                    quanLyMangCapNuoctoolStrip.Visible = false;
                    banDotoolStrip.Visible = true;
                    quanLySuCotoolStrip.Visible = false;
                    keHoachBaoDuongtoolStrip.Visible = false;
                    thongKeTaiSantoolStrip.Visible = false;
                    thongTinVatTutoolStrip.Visible = false;
                }
            }

        private void quanLyMangCapNuocToolStripMenuItem_Click(object sender, EventArgs e)
            {
            if (quanLyMangCapNuoctoolStrip.Visible == false)
                {
                    quanLyMangCapNuoctoolStrip.Visible = true;
                    banDotoolStrip.Visible = false;
                    quanLySuCotoolStrip.Visible = false;
                    keHoachBaoDuongtoolStrip.Visible = false;
                    thongKeTaiSantoolStrip.Visible = false;
                    thongTinVatTutoolStrip.Visible = false;
                }
            }

        private void quanLySuCoToolStripMenuItem_Click(object sender, EventArgs e)
            {
            if (quanLySuCotoolStrip.Visible == false)
                {
                    quanLyMangCapNuoctoolStrip.Visible = false;
                    banDotoolStrip.Visible = false;
                    quanLySuCotoolStrip.Visible = true;
                    keHoachBaoDuongtoolStrip.Visible = false;
                    thongKeTaiSantoolStrip.Visible = false;
                    thongTinVatTutoolStrip.Visible = false;
                }
            }

        private void keHoachBaoDuongToolStripMenuItem_Click(object sender, EventArgs e)
            {
            if (keHoachBaoDuongtoolStrip.Visible == false)
                {
                    quanLyMangCapNuoctoolStrip.Visible = false;
                    banDotoolStrip.Visible = false;
                    quanLySuCotoolStrip.Visible = false;
                    keHoachBaoDuongtoolStrip.Visible = true;
                    thongKeTaiSantoolStrip.Visible = false;
                    thongTinVatTutoolStrip.Visible = false;
                }
            }

        private void thongKeTaiSanToolStripMenuItem_Click(object sender, EventArgs e)
            {
            if (thongKeTaiSantoolStrip.Visible == false)
                {
                    quanLyMangCapNuoctoolStrip.Visible = false;
                    banDotoolStrip.Visible = false;
                    quanLySuCotoolStrip.Visible = false;
                    keHoachBaoDuongtoolStrip.Visible = false;
                    thongKeTaiSantoolStrip.Visible = true;
                    thongTinVatTutoolStrip.Visible = false;
                }
            }

        private void thongTinVatTuƯToolStripMenuItem_Click(object sender, EventArgs e)
            {
            if (thongTinVatTutoolStrip.Visible == false)
                {
                    quanLyMangCapNuoctoolStrip.Visible = false;
                    banDotoolStrip.Visible = false;
                    quanLySuCotoolStrip.Visible = false;
                    keHoachBaoDuongtoolStrip.Visible = false;
                    thongKeTaiSantoolStrip.Visible = false;
                    thongTinVatTutoolStrip.Visible = true;
                }
            }

        private void dangNhapToolStripMenuItem_Click(object sender, EventArgs e)
            {
            var help = new Helper();
            string str = "";
            str = help.getConfigInTextFile();

            string[] array;
            array = str.Split(',');

            if (array.Length > 4)
                {
                    var frm = new frmLoginServer();
                    frm.loadConfig(array[3]);
                    frm.ShowDialog();
                    if (axMapControl1.LayerCount != 0)
                        {
                            layerAddLabeltoolStripComboBox.Items.Clear();
                            //layerEditortoolStripDropDownButton.Items.Clear();
                            for (int i = 0; i <= axMapControl1.LayerCount - 1; i++)
                                {
                                    layerAddLabeltoolStripComboBox.Items.Insert(i, axMapControl1.get_Layer(i).Name);
                                    //layerEditortoolStripDropDownButton.Items.Insert(i, axMapControl1.get_Layer(i).Name);
                                }

                            //Select first layer in control
                            layerAddLabeltoolStripComboBox.SelectedIndex = 0;
                            //layerEditortoolStripDropDownButton.SelectedIndex = 0;

                            //scaleMaptoolStripComboBox.SelectedIndex = 0;
                            scaleMaptoolStripComboBox.Text = "1:" + axMapControl1.MapScale.ToString("#,##0");
                        }
                }
            else
                {
                    help = null;
                    var frm = new frmDangNhapHeThong();
                    frm.ShowDialog();
                }
            }

        private void CauHinhKetNoiToolStripMenuItem_Click(object sender, EventArgs e)
            {
            var frm = new frmDangNhapHeThong();
            frm.ShowDialog();
            }

        private void dangXuatToolStripMenuItem_Click(object sender, EventArgs e)
            {
            }

        private void quanLyTaiKhoanToolStripMenuItem_Click(object sender, EventArgs e)
            {
            var frm = new frmQuanLyTaiKhoan();
            frm.ShowDialog();
            }

        private void quanLyQuyenToolStripMenuItem_Click(object sender, EventArgs e)
            {
            var frm = new frmQuanLyQuyen();
            frm.ShowDialog();
            }

        private void phanQuyenTaiKhoanToolStripMenuItem_Click(object sender, EventArgs e)
            {
            var frm = new frmPhanQuyenTheoNhom();
            frm.ShowDialog();
            }

        private void suaThongTinTaiKhoanToolStripMenuItem_Click(object sender, EventArgs e)
            {
            var frm = new frmSuaThongTinTaiKhoan();
            frm.ShowDialog();
            }

        private void lichSuTruyCapHeThongToolStripMenuItem_Click(object sender, EventArgs e)
            {
            var frm = new frmXemNhatKyHeThong();
            frm.ShowDialog();
            }

        private void saoLuuDuLieuToolStripMenuItem_Click(object sender, EventArgs e)
            {
            var frm = new frmSaoLuDuLieu();
            frm.ShowDialog();
            }

        private void phucHoiDuLieuToolStripMenuItem_Click(object sender, EventArgs e)
            {
            var frm = new frmPhucHoiDuLieu();
            frm.ShowDialog();
            }

        private void quanLyThamSoHeThongToolStripMenuItem_Click(object sender, EventArgs e)
            {
            var frm = new frmThongSoHeThong();
            frm.ShowDialog();
            }

        private void thoatToolStripMenuItem_Click(object sender, EventArgs e)
            {
            //Application.Exit();
            Close();
            }
        private void MainForm_Load(object sender, EventArgs e)
        {

            //Việt hóa tocControl
            IMapControl4 pMapcontrol;
            ITOCControl pTOCControl;
            IMap pMap;
            generalVariable.mapControl = (IMapControl3)axMapControl1.Object;
            generalVariable.mapControl2 = (IMapControl3)axMapControl2.Object;
            generalVariable.pageLayoutControl = (IPageLayoutControl3)axPageLayoutControl1.Object;
            m_pageLayoutControl = (IPageLayoutControl2)axPageLayoutControl1.Object;
            m_controlsSynchronizer = new ControlsSynchronizer(generalVariable.mapControl, m_pageLayoutControl);
            Editor = new EngineEditorClass();
            //bind the controls together (both point at the same map) and set the MapControl as the active control
            m_controlsSynchronizer.BindControls(true);

            //add the framework controls (TOC and Toolbars) in order to synchronize then when the
            //active control changes (call SetBuddyControl)
            m_controlsSynchronizer.AddFrameworkControl(axTOCControl1.Object);

            m_tocControl = (ITOCControl2)axTOCControl1.Object;
            m_mapControl = (IMapControl3)axMapControl1.Object;

            //Set buddy control
            m_tocControl.SetBuddyControl(m_mapControl);


            ITool toolselectfeature = null;
            toolselectfeature = new SelectFeatures();
            axToolbarControl1.AddItem(toolselectfeature, -1, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly);

            cmdUpdateInfo = new DIWAGIS.ApplicationLayer.Commands.cmdUpdateInfo();
            cmdUpdateInfo.editor = Editor;

            cmdUpdateInfo.owner = this;
            cmdUpdateInfo.OnCreate(axMapControl1.Object);
            axToolbarControl1.AddItem(cmdUpdateInfo, -1, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly);

           
            //Add custom commands to the map menu
            m_menuMap = new ToolbarMenuClass();
            m_menuMap.AddItem(new LayerVisibility(), 1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuMap.AddItem(new LayerVisibility(), 2, 1, false, esriCommandStyles.esriCommandStyleTextOnly);
            //Add pre-defined menu to the map menu as a sub menu 
            m_menuMap.AddSubMenu("esriControls.ControlsFeatureSelectionMenu", 2, true);
            //Add custom commands to the map menu
            m_menuLayer = new ToolbarMenuClass();
            m_menuLayer.AddItem(new RemoveLayer(), -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new ScaleThresholds(), 1, 1, true, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new ScaleThresholds(), 2, 2, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new ScaleThresholds(), 3, 3, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new LayerSelectable(), 1, 4, true, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new LayerSelectable(), 2, 5, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new ZoomToLayer(), -1, 6, true, esriCommandStyles.esriCommandStyleTextOnly);

            //Set the hook of each menu
            m_menuLayer.SetHook(m_mapControl);
            m_menuMap.SetHook(m_mapControl);

            pMapcontrol = axMapControl1.Object as IMapControl4;
            pMap = pMapcontrol.Map;
            pMapcontrol.DocumentFilename = "Lớp dữ liệu";
            pMap.Name = "Lớp dữ liệu";
            pTOCControl = axTOCControl1.Object as ITOCControl;
            pTOCControl.Update();

            //Control for ToolBar
            this.banDotoolStrip.Visible = false;
            this.quanLyMangCapNuoctoolStrip.Visible = false;
            this.quanLySuCotoolStrip.Visible = false;
            this.keHoachBaoDuongtoolStrip.Visible = false;
            this.thongKeTaiSantoolStrip.Visible = false;
            this.thongTinVatTutoolStrip.Visible = false;

            //Control for Map Object 
            axSymbologyControl1.Visible = false;
            axMapControl2.Visible = false;

            //MapControl
            //axMapControl1.Map.Name = "Lớp dữ liệu";

            //statusBar
             heToaDolabel.Text = "Kinh độ: 0 ; Vĩ Độ: 0" + " | Hệ tọa độ: chưa biết";

            //Standard ToolBar Control
            scaleMaptoolStripComboBox.Text = "";

            //Ẩn/hiện Editor Map Toolbar
            addLabeltoolStrip.Visible = false;

            toolStripContainer1.RightToolStripPanel.Visible = true;


            //Công cụ ContextMenuStrip của TOC
            m_mapControl = (IMapControl3)axMapControl1.Object;
            m_tocControl = (ITOCControl2)axTOCControl1.Object;
            using (frmDangNhapHeThong dlg = new frmDangNhapHeThong())
            {

                if (dlg.ShowDialog() == DialogResult.OK)
                {

                }
            }
                     

        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult = MessageBox.Show(this, "Bạn có thực sự muốn thoát?", "Cảnh báo!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            switch (DialogResult)
            {
                case DialogResult.Cancel:
                    e.Cancel = true;
                    break;
                case DialogResult.OK:
                    for (int i = 0; i < this.OwnedForms.GetLength(0); i++)
                    {
                        (this.OwnedForms.GetValue(i) as Form).Dispose();
                    }
                    this.Dispose();
                    break;
                default:
                    break;
            }
        }
        private void buttonCloseOpenLegend_Click(object sender, EventArgs e)
        {
            if (splitContainer1.Panel1Collapsed == false)
            {
                splitContainer1.Panel1Collapsed = true;
                axMapControl1.Refresh();
                buttonCloseOpenLegend.ImageIndex = 3;
            }
            else
            {
                splitContainer1.Panel1Collapsed = false;
                buttonCloseOpenLegend.ImageIndex = 2;
                axMapControl1.Refresh();
            }
        }
        private void buttonOpenMiniMap_Click(object sender, EventArgs e)
        {
            if (axMapControl2.Visible == false)
            {
                axMapControl2.Visible = true;
                buttonOpenMiniMap.ImageIndex = 1;
            }
            else
            {
                axMapControl2.Visible = false;
                buttonOpenMiniMap.ImageIndex = 0;
            }
        }
        #region Bản đồ

        private void moBanDotoolStripButton_Click(object sender, EventArgs e)
            {
            var openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Title = "Mở bản đồ";
            openFileDialog1.Filter = "Tập tin bản đồ (*.mxd)|*.mxd";
            openFileDialog1.ShowDialog();
            //MessageBox.Show("1");
            //Exit if no map document is selected
            string sFilePath = openFileDialog1.FileName;
            if (sFilePath == "")
                {
                    return;
                }

            //Validate and load map document
            if (axMapControl1.CheckMxFile(sFilePath))
                {
                    axMapControl1.LoadMxFile(sFilePath); //, Type.Missing, "");
                    //TODO: NOMinimap
                    /* //Kiểm tra thử nghiệm MiniMap
            axMapControl2.AddLayer(axMapControl1.get_Layer(21));
            axMapControl2.Extent = axMapControl1.get_Layer(21).AreaOfInterest;
            axMapControl2.AutoMouseWheel = false;*/
                    //Enabled MapControl
                    axMapControl1.Enabled = true;

                    var m_featureLayer = axMapControl1.get_Layer(1) as IFeatureLayer;
                    if (m_featureLayer != null)
                        {
                            var m_dataset = m_featureLayer.FeatureClass as IDataset;
                            //Raster ko thể cast thành IFeatureLayer
                        }

                    //Class.Public_Variables.workspace_GDB = m_dataset.Workspace;
                    ClassVariables.f_MapControl = axMapControl1.Map;
                }
            else
                {
                    MessageBox.Show(sFilePath + " is not a valid ArcMap document");
                    return;
                }

            //Add the layer names to toolStripComboLayers
            layerAddLabeltoolStripComboBox.Items.Clear();
            //layerEditortoolStripDropDownButton.Items.Clear();
            for (int i = 0; i <= axMapControl1.LayerCount - 1; i++)
                {
                    layerAddLabeltoolStripComboBox.Items.Insert(i, axMapControl1.get_Layer(i).Name);
                    //layerEditortoolStripDropDownButton.Items.Insert(i, axMapControl1.get_Layer(i).Name);
                }

            //Select first layer in control
            layerAddLabeltoolStripComboBox.SelectedIndex = 0;
            //layerEditortoolStripDropDownButton.SelectedIndex = 0;

            //scaleMaptoolStripComboBox.SelectedIndex = 0;
            scaleMaptoolStripComboBox.Text = "1:" + axMapControl1.MapScale.ToString("#,##0");

            //Việt hóa tocControl
            IMapControl4 pMapcontrol;
            ITOCControl pTOCControl;
            IMap pMap;
            pMapcontrol = axMapControl1.Object as IMapControl4;
            pMap = pMapcontrol.Map;
            pMapcontrol.DocumentFilename = "Lớp dữ liệu";
            pMap.Name = "Lớp dữ liệu";
            pTOCControl = axTOCControl1.Object as ITOCControl;
            pTOCControl.Update();
            }
        private void luuBanDotoolStripButton_Click(object sender, EventArgs e)
            {
            try
                {
                    //make sure that the current MapDoc is valid first
                    m_mapControl = axMapControl1.Object as IMapControl4;
                    m_pageLayoutControl = axPageLayoutControl1.Object as IPageLayoutControl3;
                    if (m_documentFileName != string.Empty && m_mapControl.CheckMxFile(m_documentFileName))
                        {
                            //create a new instance of a MapDocument class
                            IMapDocument mapDoc = new MapDocumentClass();
                            //Open the current document into the MapDocument
                            mapDoc.Open(m_documentFileName, string.Empty);

                            //Replace the map with the one of the PageLayout
                            mapDoc.ReplaceContents((IMxdContents) m_pageLayoutControl.PageLayout);

                            //save the document
                            mapDoc.Save(mapDoc.UsesRelativePaths, false);
                            mapDoc.Close();
                        }
                }
            catch (Exception ex)
                {
                    clsStaticFunctions.ThongBaoLoi(ex);
                }
            }
        private void themDuLieutoolStripDropDownButton_Click(object sender, EventArgs e)
            {
            }
        private void themShapeFileToolStripMenuItem_Click(object sender, EventArgs e)
            {
            try
                {
                    var openFile = new OpenFileDialog();
                    openFile.Title = "Thêm dữ liệu Shapefile vào bản đồ";
                    openFile.Filter = "(Dữ liệu shapefile)|*.shp"; // "(*.jpg)|*.jpg|(*.png)|*.png";
                    if (openFile.ShowDialog() == DialogResult.OK)
                        {
                            if (openFile.FileName != string.Empty)
                                {
                                    string outFileShortName = Path.GetFileNameWithoutExtension(openFile.FileName);
                                    string outFilePath = Path.GetDirectoryName(openFile.FileName);
                                    axMapControl1.AddShapeFile(outFilePath, outFileShortName);
                                    //axMapControl.AddLayerFromFile(openFile.FileName, 0);
                                }
                        }
                }
            catch (Exception ex)
                {
                    clsStaticFunctions.ThongBaoLoi(ex);
                }
            }

        private void themCADFileToolStripMenuItem_Click(object sender, EventArgs e)
            {
            try
                {
                    var dialog = new OpenFileDialog();
                    //dialog.InitialDirectory = @"C:\";
                    dialog.Filter = "(File CAD)|*.dwg|(File DGN)|*.dgn";
                    dialog.Title = "Mở file CAD";
                    dialog.Multiselect = true;
                    if (dialog.ShowDialog() == DialogResult.OK)
                        {
                            if (dialog.FileName != string.Empty)
                                {
                                    //Duyệt qua từng file nếu người dùng chọn nhiều file
                                    for (int j = 0; j < dialog.SafeFileNames.Length; j++)
                                        {
                                            string FileShortName = Path.GetFileName(dialog.SafeFileNames[j]);
                                            string FilePath = Path.GetDirectoryName(dialog.FileName);
                                            var objAddCad = new clsTuongTacBanDo();
                                            objAddCad.AddCadFileToMap(axMapControl1.Object as IMapControl4, FilePath,
                                                                      FileShortName);
                                        }
                                    axMapControl1.ActiveView.Refresh();
                                }
                        }
                }
            catch (Exception ex)
                {
                    clsStaticFunctions.ThongBaoLoi(ex);
                }
            }

        private void themRasterToolStripMenuItem_Click(object sender, EventArgs e)
            {
            try
                {
                    var dialog = new OpenFileDialog();
                    //dialog.InitialDirectory = @"C:\";
                    dialog.Filter = "File Raster (*.bmp, *.jpg, *.png)|*.bmp;*.jpg;*.png";
                    dialog.Title = "Mở file Raster";
                    dialog.Multiselect = true;
                    if (dialog.ShowDialog() == DialogResult.OK)
                        {
                            if (dialog.FileName != string.Empty)
                                {
                                    //Duyệt qua từng file nếu người dùng chọn nhiều file
                                    for (int j = 0; j < dialog.SafeFileNames.Length; j++)
                                        {
                                            string FileShortName = Path.GetFileName(dialog.SafeFileNames[j]);
                                            string FilePath = Path.GetDirectoryName(dialog.FileName);
                                            var objAddCad = new clsTuongTacBanDo();
                                            objAddCad.AddRasterToMap(axMapControl1.Object as IMapControl4, FilePath,
                                                                     FileShortName);
                                        }
                                    axMapControl1.ActiveView.Refresh();
                                }
                        }
                }
            catch (Exception ex)
                {
                    clsStaticFunctions.ThongBaoLoi(ex);
                }
            }

        private void xuatBanDotoolStripButton_Click(object sender, EventArgs e)
            {
            folderBrowserDialogOpenMap.ShowNewFolderButton = true;
            folderBrowserDialogOpenMap.Description = "Chọn thư mục lưu tập tin Bản đồ:";
            DialogResult dr = folderBrowserDialogOpenMap.ShowDialog();
            switch (dr)
                {
                    case DialogResult.Cancel:
                        break;
                    case DialogResult.OK:
                        ExportActiveViewParameterized(300, 1, "JPEG", folderBrowserDialogOpenMap.SelectedPath + "\\",
                                                      false);
                        break;
                    default:
                        break;
                }
            }

        private void ExportActiveViewParameterized(long iOutputResolution, long lResampleRatio, string ExportType,
                                                   string sOutputDir, Boolean bClipToGraphicsExtent)
            {
            IExport docExport;
            IPrintAndExport docPrintExport;
            IOutputRasterSettings RasterSettings;
            string sNameRoot;
            bool bReenable = false;

            if (GetFontSmoothing())
                {
                    /* font smoothing is on, disable it and set the flag to reenable it later. */
                    bReenable = true;
                    DisableFontSmoothing();
                    if (GetFontSmoothing())
                        {
                            //font smoothing is NOT successfully disabled, error out.
                            return;
                        }
                    //else font smoothing was successfully disabled.
                }

            // The Export*Class() type initializes a new export class of the desired type.
            if (ExportType == "PDF")
                {
                    docExport = new ExportPDFClass();
                }
            else if (ExportType == "EPS")
                {
                    docExport = new ExportPSClass();
                }
            else if (ExportType == "AI")
                {
                    docExport = new ExportAIClass();
                }
            else if (ExportType == "BMP")
                {
                    docExport = new ExportBMPClass();
                }
            else if (ExportType == "TIFF")
                {
                    docExport = new ExportTIFFClass();
                }
            else if (ExportType == "SVG")
                {
                    docExport = new ExportSVGClass();
                }
            else if (ExportType == "PNG")
                {
                    docExport = new ExportPNGClass();
                }
            else if (ExportType == "GIF")
                {
                    docExport = new ExportGIFClass();
                }
            else if (ExportType == "EMF")
                {
                    docExport = new ExportEMFClass();
                }
            else if (ExportType == "JPEG")
                {
                    docExport = new ExportJPEGClass();
                }
            else
                {
                    MessageBox.Show("Unsupported export type " + ExportType + ", defaulting to EMF.");
                    ExportType = "EMF";
                    docExport = new ExportEMFClass();
                }

            docPrintExport = new PrintAndExportClass();

            //set the name root for the export
            sNameRoot = "BanDo";

            //set the export filename (which is the nameroot + the appropriate file extension)
            docExport.ExportFileName = sOutputDir + sNameRoot + "." +
                                       docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0];

            //Output Image Quality of the export.  The value here will only be used if the export
            // object is a format that allows setting of Output Image Quality, i.e. a vector exporter.
            // The value assigned to ResampleRatio should be in the range 1 to 5.
            // 1 corresponds to "Best", 5 corresponds to "Fast"

            // check if export is vector or raster
            if (docExport is IOutputRasterSettings)
                {
                    // for vector formats, assign the desired ResampleRatio to control drawing of raster layers at export time   
                    RasterSettings = (IOutputRasterSettings) docExport;
                    RasterSettings.ResampleRatio = (int) lResampleRatio;

                    // NOTE: for raster formats output quality of the DISPLAY is set to 1 for image export 
                    // formats by default which is what should be used
                }

            docPrintExport.Export(axMapControl1.ActiveView, docExport, iOutputResolution, bClipToGraphicsExtent, null);

            MessageBox.Show(
                "Đã xuất bản đồ tại:" + sOutputDir + sNameRoot + "." +
                docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0] + ".", "Xuất tập tin bản đồ");

            if (bReenable)
                {
                    /* reenable font smoothing if we disabled it before */
                    EnableFontSmoothing();
                    bReenable = false;
                    if (!GetFontSmoothing())
                        {
                            //error: cannot reenable font smoothing.
                            MessageBox.Show("Không thể làm trơn Font", "Quá trình làm trơn Font bị lỗi");
                        }
                }
            }

        private Boolean GetFontSmoothing()
            {
            bool iResult;
            int pv = 0;
            iResult = SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, ref pv, 0);

            if (pv > 0)
                {
                    //pv > 0 means font smoothing is ON.
                    return true;
                }
            else
                {
                    //pv == 0 means font smoothing is OFF.
                    return false;
                }
            }

        private void EnableFontSmoothing()
            {
            bool iResult;
            int pv = 0;

            iResult = SystemParametersInfo(SPI_SETFONTSMOOTHING, 1, ref pv, SPIF_UPDATEINIFILE);
            }

        private void DisableFontSmoothing()
            {
            bool iResult;
            int pv = 0;

            iResult = SystemParametersInfo(SPI_SETFONTSMOOTHING, 0, ref pv, SPIF_UPDATEINIFILE);
            }

        private void timKiemTheoThuocTinhtoolStripButton_Click(object sender, EventArgs e)
            {
            var frm = new frmTimKiemTheoThuocTinh();
            frm.Show(this);
            }

        private void timKiemTheoKhongGiantoolStripButton_Click(object sender, EventArgs e)
            {
            var frm = new frmTimKiemTheoKhongGian();
            frm.Show(this);
            }

        private void xemThongTinThuocTinhLoptoolStripButton_Click(object sender, EventArgs e)
            {
                var frm = new frmIndentifyFeature();
            frm.Show(this);
            }

        private void xemHinhAnhtoolStripButton_Click(object sender, EventArgs e)
            {
            var frm = new frmXemHinhAnhDoiTuong();
            frm.Show(this);
            }

        private void xemBanVetoolStripButton_Click(object sender, EventArgs e)
            {
            }

        private void xemToaDotoolStripButton_Click(object sender, EventArgs e)
            {
            }

        private void ketNapDuLieuGPStoolStripButton_Click(object sender, EventArgs e)
            {
            }

        private void capNhatMaVungQuanLytoolStripButton_Click(object sender, EventArgs e)
            {
            }

        private void gopVungQuanLytoolStripButton_Click(object sender, EventArgs e)
            {
            }

        private void tachVungQuanLytoolStripButton_Click(object sender, EventArgs e)
            {
            }

        #endregion Bản đồ
        #region Hồ sơ

        private void hoSoQuyetToantoolStripButton_Click(object sender, EventArgs e)
            {
            var frm = new frmQuanLyHoSoQuyetToan();
            frm.Show(this);
            }

        private void dongVanDieuKhientoolStripButton_Click(object sender, EventArgs e)
            {
            var frm = new frmDongVan();
            frm.Show(this);
            }

        private void moVanDieuKhientoolStripButton_Click(object sender, EventArgs e)
            {
            var frm = new frmMoVan();
            frm.Show(this);
            }

        private void danhSachVanDangDongtoolStripButton_Click(object sender, EventArgs e)
            {
            var frm = new frmDanhSachVanDangDong();
            frm.Show(this);
            }

        private void lichSuVanHanhVantoolStripButton_Click(object sender, EventArgs e)
            {
            var frm = new frmNhatKyVanHanhVan();
            frm.Show(this);
            }

        private void lichSuKHAnhHuongSuCoDongVantoolStripButton_Click(object sender, EventArgs e)
            {
            var frm = new frmTraCuuLSKHMatNuoc();
            frm.Show(this);
            }

        private void moPhongDongMoVantoolStripButton_Click(object sender, EventArgs e)
            {
            var frm = new frmMoPhongDongMoVan();
            frm.Show(this);
            }

        private void toolStripButtonNhapThongTinKhachHang_Click(object sender, EventArgs e)
            {
            var frm = new frmNhapDuLieuKhachHang();
            frm.Show(this);
            }

        private void nhapChiSoNuocTieuThutoolStripButton_Click(object sender, EventArgs e)
            {
            }

        private void nhapDuLieuDataloggertoolStripButton_Click(object sender, EventArgs e)
            {
            var frm = new frmNhapDuLieuDataLogger();
            frm.Show(this);
            }

        private void tinhThatThoattoolStripButton_Click(object sender, EventArgs e)
            {
            var frm = new frmTinhTyLeThatThoat();
            frm.Show(this);
            }

        #endregion
        #region Thống kê báo cáo
        private void ongChinhToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmThongKeDuongOng frm = new frmThongKeDuongOng();
            frm.Text = "Báo cáo hiện trạng đường Ống Chính";
            frm.Show(this);
        }

        private void ongNhanhToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmThongKeDuongOngNhanh frm = new frmThongKeDuongOngNhanh();
            frm.Text = "Báo cáo hiện trạng đường Ống Nhánh";
            frm.Show(this);
        }

        private void dongHoTongToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmThongKeDongHoTong frm = new frmThongKeDongHoTong();
            frm.Show(this);
        }

        private void dongHoKhachHangToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmThongKeDongHoKhachHang frm = new frmThongKeDongHoKhachHang();
            frm.Show(this);
        }

        private void vanDieuKhienToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmThongKeVan frm = new frmThongKeVan();
            frm.Text = "Báo cáo hiện trạng Van";
            frm.Show(this);
        }
        #endregion
        #region Công cụ thao tác bản đồ

        private void zoomIntoolStripButton_Click(object sender, EventArgs e)
            {
            try
                {
                    var objTuongTacBanDo = new clsTuongTacBanDo();
                    objTuongTacBanDo.ZoomIn(axMapControl1.Object as IMapControl4);
                }
            catch (Exception ex)
                {
                    clsStaticFunctions.ThongBaoLoi(ex);
                }

            zoomIntoolStripButton.Checked = true;
            zoomOuttoolStripButton.Checked = false;
            pantoolStripButton.Checked = false;
            }

        private void zoomOuttoolStripButton_Click(object sender, EventArgs e)
            {
            try
                {
                    var objTuongTacBanDo = new clsTuongTacBanDo();
                    objTuongTacBanDo.ZoomOut(axMapControl1.Object as IMapControl4);
                }
            catch (Exception ex)
                {
                    clsStaticFunctions.ThongBaoLoi(ex);
                }

            zoomIntoolStripButton.Checked = false;
            zoomOuttoolStripButton.Checked = true;
            pantoolStripButton.Checked = false;
            }

        private void pantoolStripButton_Click(object sender, EventArgs e)
            {
            try
                {
                    var objTuongTacBanDo = new clsTuongTacBanDo();
                    objTuongTacBanDo.PanMap(axMapControl1.Object as IMapControl4);
                }
            catch (Exception ex)
                {
                    clsStaticFunctions.ThongBaoLoi(ex);
                }

            zoomIntoolStripButton.Checked = false;
            zoomOuttoolStripButton.Checked = false;
            pantoolStripButton.Checked = true;
            }

        private void fullExtenttoolStripButton_Click(object sender, EventArgs e)
            {
            try
                {
                    var zommL = new ConfigInterface();
                    zommL.ZoomtoLayerName(generalVariable.database + "." + generalVariable.schema + "." + "HanhChinh",
                                          generalVariable.ICurrentWorkspace, generalVariable.mapControl, "HanhChinh");
                }
            catch (Exception ex)
                {
                    clsStaticFunctions.ThongBaoLoi(ex);
                }
            }

        private void pointertoolStripButton_Click(object sender, EventArgs e)
            {
            axMapControl1.CurrentTool = null;
            zoomIntoolStripButton.Checked = false;
            zoomOuttoolStripButton.Checked = false;
            pantoolStripButton.Checked = false;
            m_IdentifyFeature = null;
            }

        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            axMapControl1.Focus();
            scaleMaptoolStripComboBox.Text = "1:" + axMapControl1.MapScale.ToString("#,##0");
        }

        private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
        {

            if (axMapControl1.LayerCount == 0) return;
            Class.ConvertXYToLongLat m_ConvertXYToLongLat = new Class.ConvertXYToLongLat();
            int Zone = 48; //Tùy biến theo địa phương
            m_ConvertXYToLongLat.ToLatLon(e.mapX, e.mapY, Zone);
            heToaDolabel.Text = "Kinh độ: " + m_ConvertXYToLongLat.Longitude.ToString("##.#####") + " ; Vĩ độ: " + m_ConvertXYToLongLat.Latitude.ToString("##.#####") + " | Hệ tọa độ: " + axMapControl1.Map.SpatialReference.Name.ToString();
            scaleMaptoolStripComboBox.Text = "1:" + axMapControl1.MapScale.ToString("#,##0");
        }

        private void toolStripButtonPreviousExtent_Click(object sender, EventArgs e)
        {
            try
            {
                clsTuongTacBanDo objTuongTacBanDo = new clsTuongTacBanDo();
                objTuongTacBanDo.PreviousExtent(axMapControl1.Object as IMapControl4);
            }
            catch (Exception ex)
            {
                Class.clsStaticFunctions.ThongBaoLoi(ex);
            }
        }

        private void toolStripButtonNextExtent_Click(object sender, EventArgs e)
        {
            try
            {
                clsTuongTacBanDo objTuongTacBanDo = new clsTuongTacBanDo();
                objTuongTacBanDo.NextExtent(axMapControl1.Object as IMapControl4);
            }
            catch (Exception ex)
            {
                Class.clsStaticFunctions.ThongBaoLoi(ex);
            }
        }

        private void fixedZoomIntoolStripButton_Click(object sender, EventArgs e)
        {
            IEnvelope ipEnv;
            ipEnv = axMapControl1.Extent;
            ipEnv.Expand(0.5, 0.5, true);
            axMapControl1.Extent = ipEnv;
            scaleMaptoolStripComboBox.Text = "1:" + axMapControl1.MapScale.ToString("#,##0");
        }

        private void fixedZoomOuttoolStripButton_Click(object sender, EventArgs e)
        {
            IEnvelope ipEnv;
            ipEnv = axMapControl1.Extent;
            ipEnv.Expand(2, 2, true);
            axMapControl1.Extent = ipEnv;
            scaleMaptoolStripComboBox.Text = "1:" + axMapControl1.MapScale.ToString("#,##0");
        }

        private void selectedObjecttoolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                clsTuongTacBanDo objTuongTacBanDo = new clsTuongTacBanDo();
                objTuongTacBanDo.SelectFeatures(axMapControl1.Object as IMapControl4);
            }
            catch (Exception ex)
            {
                Class.clsStaticFunctions.ThongBaoLoi(ex);
            }
        }

        private void unselectObjecttoolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                clsTuongTacBanDo objTuongTacBanDo = new clsTuongTacBanDo();
                objTuongTacBanDo.ClearSelection(axMapControl1.Object as IMapControl4);
            }
            catch (Exception ex)
            {
                Class.clsStaticFunctions.ThongBaoLoi(ex);
            }
        }

        private void clearSelectAlltoolStripButton_Click(object sender, EventArgs e)
        {
            if (axMapControl1.Map.LayerCount == 0) return;
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {
                IFeatureLayer featureLayer = (IFeatureLayer)axMapControl1.get_Layer(i);
                featureLayer.Selectable = false;
            }
        }

        private void setupSelectAlltoolStripButton_Click(object sender, EventArgs e)
        {
            if (axMapControl1.Map.LayerCount == 0) return;
            int i;
            for (i = 0; i < axMapControl1.LayerCount; i++)
            {
                IFeatureLayer featureLayer = (IFeatureLayer)axMapControl1.get_Layer(i);
                featureLayer.Selectable = true;
            }
        }

        private void indentifytoolStripButton_Click(object sender, EventArgs e)
        {
            //DO NOTHING
        }

        private void hyperLinktoolStripButton_Click(object sender, EventArgs e)
        {
            //DO NOTHING
        }

        private void rulertoolStripButton_Click(object sender, EventArgs e)
        {

            ICommand cmd1 = new ControlsMapMeasureToolClass();
            cmd1.OnCreate(axMapControl1.Object);
            axMapControl1.CurrentTool = cmd1 as ITool;
        }

        private void findtoolStripButton_Click(object sender, EventArgs e)
        {
            ICommand cmd1 = new ControlsMapFindCommandClass();
            cmd1.OnCreate(axMapControl1.Object);
            cmd1.OnClick();
        }

        private void goToXYtoolStripButton_Click(object sender, EventArgs e)
        {
            ICommand cmd1 = new ControlsMapGoToCommandClass();
            cmd1.OnCreate(axMapControl1.Object);
            cmd1.OnClick();
        }

        private void addLabeltoolStripButton_Click(object sender, EventArgs e)
        {
            if (addLabeltoolStripButton.Checked == true)
            {
                addLabeltoolStrip.Visible = true;
            }
            else
            {
                addLabeltoolStrip.Visible = false;
            }
        }

        private void batDauHieuChinhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editing = new DIWAGIS.Class.Editing();
            try
            {
                WorkspaceEdit = null;
                axMapControl1.CurrentTool = null;

                //Load những lớp dữ liệu vào combox TargetLayer
                string selectedText = layerEditortoolStripDropDownButton.Text;
                layerEditortoolStripDropDownButton.Items.Clear();
                IList<ILayer> lstLayer = DIWAGIS.Class.WorkingWithLayer.GetAllLayer(axMapControl1.Object as IMapControl3);



                for (int i = 0; i < lstLayer.Count; i++)
                {
                    layerEditortoolStripDropDownButton.Items.Add(lstLayer[i].Name);
                }
                if (lstLayer.Count > 0)
                {
                    layerEditortoolStripDropDownButton.Enabled = true;
                    layerEditortoolStripDropDownButton.SelectedIndex = 0;
                    m_TargetLayer = DIWAGIS.Class.WorkingWithLayer.GetLayerByName(layerEditortoolStripDropDownButton.Text, axMapControl1.Object as IMapControl4);
                }
                if (m_TargetLayer == null)
                {
                    //DIWAGIS.Class.WorkingWithLayer.CanhBao("Bạn chưa mở bản đồ!");
                    return;
                }
                if (Editor == null)
                    return;

                ((IEngineEditLayers)Editor).SetTargetLayer(m_TargetLayer as IFeatureLayer, 0);
                editing.StartEditingFileGeodatabase(Editor, axMapControl1.Object as IMapControl4, m_TargetLayer);

                //Enable Save, Stop, Snap
                batDauHieuChinhToolStripMenuItem.Enabled = false;
                dungHieuChinhToolStripMenuItem.Enabled = true;
                luuHieuChinhToolStripMenuItem.Enabled = true;
                thietLapBatDiemToolStripMenuItem.Enabled = true;

                if (selectedText != string.Empty)
                {
                    for (int i = 0; i < layerEditortoolStripDropDownButton.Items.Count; i++)
                    {
                        if (layerEditortoolStripDropDownButton.Items[i].ToString() == selectedText)
                        {
                            layerEditortoolStripDropDownButton.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DIWAGIS.Class.clsStaticFunctions.ThongBaoLoi(ex);
                editing.StopEditing(Editor, m_TargetLayer);
            }

        }

        private void scaleMaptoolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            axMapControl1.MapScale = Convert.ToUInt32(scaleMaptoolStripComboBox.Text.ToString().Substring(2, scaleMaptoolStripComboBox.Text.ToString().Length - 2).ToString());
            axMapControl1.Refresh();
            scaleMaptoolStripComboBox.Text = "1:" + axMapControl1.MapScale.ToString("#,##0");
        }

        private void scaleMaptoolStripComboBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (DIWAGIS.Class.ClassVariables.aFuction.IsNumberInt(scaleMaptoolStripComboBox.Text.ToString()))
                {
                    axMapControl1.MapScale = Convert.ToUInt32(scaleMaptoolStripComboBox.Text.ToString());
                    axMapControl1.Refresh();
                    scaleMaptoolStripComboBox.Text = "1:" + axMapControl1.MapScale.ToString("#,##0");
                }
            }
        }

        private void editSelectiontoolStripButton_Click(object sender, EventArgs e)
        {
            ICommand m_EditTool = new ControlsEditingEditToolClass();
            m_EditTool.OnCreate(axMapControl1.Object);
            axMapControl1.CurrentTool = m_EditTool as ITool;
        }

        private void editDrawtoolStripButton_Click(object sender, EventArgs e)
        {
            ICommand m_EditSketch = new ControlsEditingSketchToolClass();
            m_EditSketch.OnCreate(axMapControl1.Object);
            axMapControl1.CurrentTool = m_EditSketch as ITool;
        }

        private void splitPolylinetoolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void splitPolygontoolStripButton_Click(object sender, EventArgs e)
        {
            ToolCutPolygon = new DIWAGIS.ApplicationLayer.EditorTools.ToolCutPolygon(Editor);
            ToolCutPolygon.OnCreate(axMapControl1.Object);
            axMapControl1.CurrentTool = ToolCutPolygon as ITool;
        }

        private void updateAttributeObjecttoolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void updateXYObjecttoolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void addVertextoolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void deleteVertextoolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void editReUndotoolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void editUndotoolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void taoDanhDauBanDoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmTaoMoiDanhDau frm = new frmTaoMoiDanhDau();
            frm.Show(this);
        }

        private void quanLyDanhDauBanDoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmQuanLyDanhDau frm = new frmQuanLyDanhDau();
            frm.Show(this);
        }

        private void layerEditortoolStripDropDownButton_Click(object sender, EventArgs e)
        {
            if (axMapControl1.LayerCount != 0)
            {
                layerEditortoolStripDropDownButton.Items.Clear();
                for (int i = 0; i <= axMapControl1.LayerCount - 1; i++)
                {
                    layerEditortoolStripDropDownButton.Items.Insert(i, axMapControl1.get_Layer(i).Name);
                }

            }
        }

        private void layerEditortoolStripDropDownButton_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (!layerEditortoolStripDropDownButton.Enabled)
            {
                return;
            }
            //Get the IEngineEditLayers
            IEngineEditLayers m_EngineEditLayers = m_EngineEditor as IEngineEditLayers;
            //Get the layer correspond to the Selected Item in the ComboBoxTargetLayer
            //ILayer m_TargetLayer = null;
            //ICompositeLayer m_CompositeLayer = axMapControl1.get_Layer(0) as ICompositeLayer;
            for (int layerIndex = 0; layerIndex <= axMapControl1.LayerCount - 1; layerIndex++)
            {

                if (layerEditortoolStripDropDownButton.SelectedItem.ToString() == axMapControl1.get_Layer(layerIndex).Name)
                {
                    m_TargetLayer = axMapControl1.get_Layer(layerIndex);
                    break;
                }
            }
            //Set target Edit Layer cho IEngineEditor
            if (m_TargetLayer != null)
            {
                m_EngineEditLayers.SetTargetLayer(m_TargetLayer as IFeatureLayer, 0);
            }
            axMapControl1.Focus();

        }

        private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            axMapControl1.Extent = axMapControl2.TrackRectangle();
            scaleMaptoolStripComboBox.Text = "1:" + axMapControl1.MapScale.ToString("#,##0");
        }

        private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
        {
            if (e.button != 2) return;
            //Determine what kind of item is selected
            m_tocControl.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);

            //Ensure the item gets selected 
            if (item != esriTOCControlItem.esriTOCControlItemNone)
            {
                if (item == esriTOCControlItem.esriTOCControlItemMap)
                    m_tocControl.SelectItem(map, null);
                else
                    m_tocControl.SelectItem(layer, null);

                //Set the layer into the CustomProperty (this is used by the custom layer commands)			
                m_mapControl.CustomProperty = layer;

                //Popup the correct context menu
                if (item == esriTOCControlItem.esriTOCControlItemMap) m_menuMap.PopupMenu(e.x, e.y, m_tocControl.hWnd);
                if (item == esriTOCControlItem.esriTOCControlItemLayer) m_menuLayer.PopupMenu(e.x, e.y, m_tocControl.hWnd);
            }
        }

        private void axTOCControl1_OnDoubleClick(object sender, ITOCControlEvents_OnDoubleClickEvent e)
        {
            //Determine if the click was over a layer item
            esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
            IBasicMap map = null;
            ILayer layer = null;
            //Object other = null, index = null;

            //Determine what kind of item is selected
            axTOCControl1.HitTest(e.x, e.y, ref item, ref  map, ref  layer, ref  other, ref  index);

            if (item == esriTOCControlItem.esriTOCControlItemLayer)
            {
                generalVariable.currentNameLayerWorking = layer.Name;
                //MessageBox.Show(layer.Name);
                //statusProcess.Text = "Bạn đang thao tác dữ liệu của lớp  : " + layer.Name;
            }
        }

        private void dungHieuChinhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (m_EngineEditor.HasEdits() == false)
                m_EngineEditor.StopEditing(false);
            else
            {

                DialogResult m_RS = MessageBox.Show("Bạn có muốn lưu thay đổi ?", "Nhắc nhở", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                switch (m_RS)
                {
                    case DialogResult.Cancel:
                        break;
                    case DialogResult.No:
                        m_EngineEditor.StopEditing(false);
                        break;
                    case DialogResult.Yes:
                        m_EngineEditor.StopEditing(true);
                        batDauHieuChinhToolStripMenuItem.Enabled = true;
                        dungHieuChinhToolStripMenuItem.Enabled = false;
                        luuHieuChinhToolStripMenuItem.Enabled = false;
                        break;
                    default:
                        break;
                }
            }
            batDauHieuChinhToolStripMenuItem.Enabled = true;
            dungHieuChinhToolStripMenuItem.Enabled = false;
            luuHieuChinhToolStripMenuItem.Enabled = false;
        }

        private void mapAndLayouttabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mapAndLayouttabControl.SelectedIndex == 0) //map view
            {
                //activate the MapControl and deactivate the PageLayoutControl
                m_controlsSynchronizer.ActivateMap();
            }else
                {
                    //activate the PageLayoutControl and deactivate the MapControl
                    frm_pagePrintSetup pagePrint;
                    pagePrint = new frm_pagePrintSetup();
                    //pagePrint._activeMap = new frm_pagePrintSetup.activeMapControl(activeMapControl);
                    pagePrint.ShowDialog();
                    m_controlsSynchronizer.ActivatePageLayout();
                }
        }

        private void toolStripMenuItemBeChua_Click(object sender, EventArgs e)
        {
            
        }

        private void toolStripMenuItemOngTruyenTai_Click(object sender, EventArgs e)
        {
           
        }

        private void donViQuanLyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (frmDMDonViQuanLy dlg = new frmDMDonViQuanLy())
            {

                if (dlg.ShowDialog() == DialogResult.OK)
                {

                }
            }
        }

        private void toolStripMenuItem14_Click(object sender, EventArgs e)
        {
            using (frmOngTruyenDan dlg = new frmOngTruyenDan())
            {

                if (dlg.ShowDialog() == DialogResult.OK)
                {

                }
            }
        }

   

    

        


        }

    #endregion

    }
